package com.roc.admin.application.service.impl;

import com.roc.admin.application.service.RoleService;
import com.roc.admin.domain.entity.Role;
import com.roc.admin.domain.repository.RoleRepository;
import com.roc.admin.domain.service.RoleDomainService;
import com.roc.admin.interfaces.dto.RoleDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 角色应用服务实现类
 */
@Service
public class RoleServiceImpl implements RoleService {

    private final RoleRepository roleRepository;
    private final RoleDomainService roleDomainService;

    public RoleServiceImpl(RoleRepository roleRepository, RoleDomainService roleDomainService) {
        this.roleRepository = roleRepository;
        this.roleDomainService = roleDomainService;
    }

    @Override
    @Transactional
    public Long createRole(RoleDTO roleDTO) {
        Role role = toRole(roleDTO);
        role = roleDomainService.createRole(role);
        return role.getId();
    }

    @Override
    @Transactional
    public boolean updateRole(RoleDTO roleDTO) {
        Role role = toRole(roleDTO);
        role = roleDomainService.updateRole(role);
        return role != null;
    }

    @Override
    @Transactional
    public boolean deleteRole(Long roleId) {
        roleDomainService.deleteRole(roleId);
        return true;
    }

    @Override
    public RoleDTO getRoleById(Long roleId) {
        Optional<Role> roleOptional = roleRepository.findById(roleId);
        return roleOptional.map(this::toRoleDTO).orElse(null);
    }

    @Override
    public List<RoleDTO> getRoleList() {
        List<Role> roleList = roleRepository.findAll();
        return roleList.stream().map(this::toRoleDTO).collect(Collectors.toList());
    }

    @Override
    public List<RoleDTO> getRoleListByUserId(Long userId) {
        List<Role> roleList = roleRepository.findByUserId(userId);
        return roleList.stream().map(this::toRoleDTO).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public boolean assignMenus(Long roleId, List<Long> menuIds) {
        roleDomainService.assignMenus(roleId, menuIds);
        return true;
    }

    @Override
    @Transactional
    public boolean assignDepts(Long roleId, List<Long> deptIds) {
        roleDomainService.assignDepts(roleId, deptIds);
        return true;
    }

    /**
     * 将角色DTO转换为角色实体
     *
     * @param roleDTO 角色DTO
     * @return 角色实体
     */
    private Role toRole(RoleDTO roleDTO) {
        if (roleDTO == null) {
            return null;
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        return role;
    }

    /**
     * 将角色实体转换为角色DTO
     *
     * @param role 角色实体
     * @return 角色DTO
     */
    private RoleDTO toRoleDTO(Role role) {
        if (role == null) {
            return null;
        }
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(role, roleDTO);
        return roleDTO;
    }
}
